[#include "../common/taglibs.html"/]
<div class="modal-header">
	<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
	<h4 class="modal-title">[@spring.message code="crawlerconfig.create.title"/]</h4>
</div>

<div class="modal-control">
	[@form.form commandName="createDTO" method="POST" class="horizontal-form form"]
	<input type="hidden" name="format" value="json">
	<input type="hidden" id="gameName" name="gameName">
	<input type="hidden" id="test" name="test">
	<div class="modal-body">
		<div class="form-body">
			<h3 class="form-section">配置信息</h3>
			<div class="row">
				<div class="col-md-4">
					<div class="form-group">
						<label class="control-label">[@spring.message code="crawlerconfig.create.form.label.gameName"/]</label>
						<div>
							[@spring.bind path="gameId"]
							<select id="${status.expression}" name="${status.expression}" class="bs-select form-control">
								[#list gameList as item]
								<option value="${item.id}">${item.name}</option>
								[/#list]
							</select>
							[/@spring.bind]
						</div>
					</div>
				</div>
				<div class="col-md-8">
					<div class="form-group">
						<label class="control-label">[@spring.message code="crawlerconfig.create.form.label.url"/]</label>
						<div>
							[@spring.bind path="url"]
							<input id="${status.expression}" name="${status.expression}" value="${status.value!}" type="text" class="form-control">
							[/@spring.bind]
						</div>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">[@spring.message code="crawlerconfig.create.form.label.expression"/]</label>
				<div>
					[@spring.bind path="expression"]
					<textarea id="${status.expression}" name="${status.expression}" type="text" class="form-control" style="height: 75px"></textarea>
					[/@spring.bind]
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">[@spring.message code="crawlerconfig.create.form.label.expression"/]</label>
				<div>
					[@spring.bind path="expression"]
					<textarea id="${status.expression}" name="${status.expression}" type="text" class="form-control" style="height: 75px"></textarea>
					[/@spring.bind]
				</div>
			</div>
			<div class="row">
				<div class="col-md-6">
					<div class="form-group">
						<label class="control-label">[@spring.message code="crawlerconfig.create.form.label.groupIndexs"/]</label>
						<div>
							[@spring.bind path="groupIndexs"]
							<input id="${status.expression}" name="${status.expression}" value="${status.value!}" type="text" class="form-control">
							[/@spring.bind]
						</div>
					</div>
				</div>
				<div class="col-md-6">
					<div class="form-group">
						<label class="control-label">[@spring.message code="crawlerconfig.create.form.label.url"/]</label>
						<div>
							[@spring.bind path="url"]
							<input id="${status.expression}" name="${status.expression}" value="${status.value!}" type="text" class="form-control">
							[/@spring.bind]
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-md-6">
					<div class="form-group">
						<label class="control-label">[@spring.message code="crawlerconfig.create.form.label.delay"/]</label>
						<div>
							[@spring.bind path="delay"]
							<input id="${status.expression}" name="${status.expression}" value="${status.value!}" type="text" class="form-control">
							[/@spring.bind]
						</div>
					</div>
				</div>
				<div class="col-md-6">
					<div class="form-group">
						<label class="control-label">[@spring.message code="crawlerconfig.create.form.label.timeout"/]</label>
						<div>
							[@spring.bind path="timeout"]
							<input id="${status.expression}" name="${status.expression}" value="${status.value!}" type="text" class="form-control">
							[/@spring.bind]
						</div>
					</div>
				</div>
			</div>
			
			
			<h3 class="form-section">测试信息</h3>
			<div class="form-group">
				<label class="control-label">期号</label>
				<div>
					<input id="issue" name="issue" type="text" class="form-control">
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">测试结果</label>
				<div>
					<textarea id="result" name="result" type="text" class="form-control" style="white-space: nowrap; height: 150px" readonly="readonly"></textarea>
				</div>
			</div>
			
		</div>
	</div>
	<div class="modal-footer">
		<button type="button" class="btn default" data-dismiss="modal">[@spring.message code="form.button.close"/]</button>
		<button id="btnTest" type="button" class="btn green">[@spring.message code="crawlerconfig.create.form.button.test"/]</button>
		<button type="submit" class="btn blue">[@spring.message code="form.button.submit"/]</button>
	</div>
	[/@form.form]
</div>

<script type="text/javascript">

$("#gameName").val($('#gameId option:selected').text());
$("#gameId").change(function(e){
	$("#gameName").val($('#gameId option:selected').text());
});

$("#btnTest").click(function(e){
	$("#test").val("true");
	$("#createDTO").submit();
});

$('#createDTO').validate({
	rules: {
  	},
	messages:{
	},
	submitHandler : function(form) {
		$modal = $('.modal-control');
		$modal.find('.modal-body > .alert').remove();
		$('.form-group').removeClass('has-error');
		$modal.modal('loading');
		
		$(form).ajaxSubmit({
			success:function(data){
				$modal.modal('loading');
				if (data.errors) {
					var msg = '';
					$.each(data.errors, function(i, val){
						if (val.field) {
							var $field = $('[name=' + val.field + ']');
							$field.closest('.form-group').removeClass('has-success').addClass('has-error');
							$field.closest('div[class^="col-md"]').append('<label for="' + val.field + '" class="help-block">' + val.message + '</label>');
						} else {
							msg += val.message;
						}
					});
					if (msg != '') {
						$modal.find('.modal-body').prepend('<div class="alert alert-danger fade in">' + msg + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
					}
				} else if (data.result) {
					$("#result").val(data.result);
				} else {
					$modal.find('.modal-body').prepend('<div class="alert alert-success fade in">' + message['operation.result.SUCCESS'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
				}
			},
			error:function(error){
				$modal.modal('loading').find('.modal-body').prepend('<div class="alert alert-danger fade in">' + message['operation.result.FAILURE'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
			}
		});
	}
});

</script>